package com.tts.hbdao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.tts.hbentity.Company;

/**
 * Data access object (DAO) for domain model class Company.
 * 
 * @see com.tts.hbentity.Company
 * @author MyEclipse Persistence Tools
 */

public class CompanyDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(CompanyDAO.class);
	// property constants
	public static final String COMPANY_NAME = "companyName";
	public static final String COMPANY_TEL = "companyTel";
	public static final String COMPANY_AREA = "companyArea";
	public static final String COMPANY_ADDRESS = "companyAddress";
	public static final String COMPANY_LEVEL = "companyLevel";
	public static final String COMPANY_INTRODUCE = "companyIntroduce";
	public static final String COMPANY_STATUS = "companyStatus";
	public static final String COMPANY_MANAGER = "companyManager";
	public static final String COMPANY_MANAGER_PWD = "companyManagerPwd";
	public static final String COMPANY_MANAGER_EMAIL = "companyManagerEmail";

	public void save(Company transientInstance) {
		log.debug("saving Company instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Company persistentInstance) {
		log.debug("deleting Company instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Company findById(java.lang.Integer id) {
		log.debug("getting Company instance with id: " + id);
		try {
			Company instance = (Company) getSession().get(
					"com.tts.hbdao.Company", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Company instance) {
		log.debug("finding Company instance by example");
		try {
			List results = getSession().createCriteria("com.tts.hbdao.Company")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Company instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Company as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCompanyName(Object companyName) {
		return findByProperty(COMPANY_NAME, companyName);
	}

	public List findByCompanyTel(Object companyTel) {
		return findByProperty(COMPANY_TEL, companyTel);
	}

	public List findByCompanyArea(Object companyArea) {
		return findByProperty(COMPANY_AREA, companyArea);
	}

	public List findByCompanyAddress(Object companyAddress) {
		return findByProperty(COMPANY_ADDRESS, companyAddress);
	}

	public List findByCompanyLevel(Object companyLevel) {
		return findByProperty(COMPANY_LEVEL, companyLevel);
	}

	public List findByCompanyIntroduce(Object companyIntroduce) {
		return findByProperty(COMPANY_INTRODUCE, companyIntroduce);
	}

	public List findByCompanyStatus(Object companyStatus) {
		return findByProperty(COMPANY_STATUS, companyStatus);
	}

	public List findByCompanyManager(Object companyManager) {
		return findByProperty(COMPANY_MANAGER, companyManager);
	}

	public List findByCompanyManagerPwd(Object companyManagerPwd) {
		return findByProperty(COMPANY_MANAGER_PWD, companyManagerPwd);
	}

	public List findByCompanyManagerEmail(Object companyManagerEmail) {
		return findByProperty(COMPANY_MANAGER_EMAIL, companyManagerEmail);
	}

	public List findAll() {
		log.debug("finding all Company instances");
		try {
			String queryString = "from Company";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Company merge(Company detachedInstance) {
		log.debug("merging Company instance");
		try {
			Company result = (Company) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Company instance) {
		log.debug("attaching dirty Company instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Company instance) {
		log.debug("attaching clean Company instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}